File Input/Output with Eddie

level: beginner

by Alyce Watson [http://alycesrestaurant.com/]


Home

Tip Corner

API Corner

Youth Corner

SpriteByte: Basics

StyleBits 3

Shaped Window

Recursion

Eddie's Lessons

Beginners Programming

Help

Index

Storing Eddie user info in a file.

Eddie has learned how to run code in Liberty BASIC by invoking liberty.exe from the command line. For more on this, see Running LB from the Command Line. We've asked the user to specify the path to liberty.exe with the methods in Filedialog with Eddie. We can either ask the user to show us the path each time Eddie is run, or we can ask once and store the information in a disk file. Let's make it easy on the user and store the location of liberty.exe in a file on disk.

Files: opening, handles, and modes.

Files are opened with the OPEN statement. The syntax is as follows:

open "filename.ext" for MODE as #handle

The "filename.ext" is a string literal or string variable containing the path and filename. If the file is in the same directory as the program, no path information is needed. The MODE can be one of the following:

The #handle can be any valid handle. It should contain only alpha-numeric characters. Some valid handles:

This article will discuss sequential files. Sequential files are opened for reading or writing from beginning to end. The modes INPUT, OUTPUT, APPEND are used for sequential files. The modes RANDOM, BINARY allow us to read or write to any part of the opened file. Eddie doesn't use these modes.

How to crash Eddie!

The easiest way to check for a file's existence is to open it and check its length. Did you know that attempting to open a nonexistent file for INPUT will cause a program to crash? If we want to crash Eddie, we can attempt to open the file containing user information for INPUT. If it doesn't exist, we've got our crash!

How NOT to crash Eddie - opening a file for APPEND.

If we try to open a nonexistent file for APPEND it is created. No crash! Files opened for APPEND can have data written to the end of them. We aren't going to write anything to the file. We're using this file-open mode simply to see if the file contains any data. To do this, we open the file, get the length with the LOF() function, and close it right away. (A future installment of Eddie will discuss appending data to a file.) If the file length is 0, the file contains no data. The LOF() function is used with an opened file and the handle assigned to the file must be inside the parentheses.

    open "eddie2.ini" for append as #f
    lenFile = LOF(#f)
    close #f

We've now got the length of the file stored in the variable lenFile. If there is data, we can open the file and read the data. If there is no data, we'll have to ask the user to set the path to liberty.exe.

    if lenFile=0 then
        'code to ask user for path to LB, since it isn't stored in disk file
    else
        'open file and read contents to find path to LB
    end if

Disk file extensions - what's this "ini" thing?

The part of a filename after the dot is called the extension. For more about extensions, see Filedialog with Eddie.

When you write files to disk, you can give them any extension you want, or no extension at all. It is best to stick to the standard rules when naming files and using extensions. You can give a text file the extension BMP. Liberty BASIC will not complain if you do that. If the user opens "My Computer" and clicks on the file, Windows will try to open it in the Paint program, because that is the program associated with bitmap images. That's why it is best to use standard extensions for your filenames.

The extension INI is short for "initialization." An INI file is almost always a plain text file that stores user information about a program. Liberty BASIC itself uses an INI file. You can open the LB ini file and read it in any text editor. INI is the perfect extension to use for Eddie's initialization file.

Storing data in a file - opening for OUTPUT.

If the user selects liberty.exe with a filedialog, Eddie can use that information to run Liberty BASIC with the code in the editor. See run. We can now save the path to liberty.exe in a disk file so that we can read it the next time Eddie opens. We must open the ini file for output. If the file does not already exist, it is created. We can write data to files opened for OUTPUT. If a file is opened for output, all previous contents are overwritten! We do the actual writing with the PRINT command. We can print literal strings, literal numbers, string variables, or numeric variables to a file that has been opened for output. Each PRINT command writes a new line of text to the open disk file.

'open a file for output
open "filename.txt" for output as #f
'print a string literal to the file
print #f, "Hello World"
'print a string variable to the file
var$ = "I love Liberty BASIC!"
print #f, var$
'print a literal number to a file
print #f, 17
'print a numeric variable to a file
num = 44
print #f, num
'close the file
close #f

If we open the file in Notepad, it looks like this:

Hello World

I Love Liberty BASIC

17

44

Suppressing new lines.

If we don't want each PRINT statement to start a new line in the file, we can suppress the new line by ending each PRINT statement with a semicolon. The code below does just that.

'open a file for output
open "filename.txt" for output as #f
'print a string literal to the file
print #f, "Hello World";
'print a string variable to the file
var$ = "I love Liberty BASIC!"
print #f, var$;
'print a literal number to a file
print #f, 17;
'print a numeric variable to a file
num = 44
print #f, num;
'close the file
close #f

The result when viewed in Notepad looks like this:

Hello WorldI love Liberty BASIC!1744

Writing Eddie's INI file

Here is Eddie's code to save the path to liberty.exe to disk:

    open "eddie2.ini" for output as #f
    print #f, LibertyExe$
    close #f

Reading data from a file - opening for INPUT.

To read data from a file, we open it for INPUT.

open "myfile.txt" for input as #f
'do stuff
close #f

Once it is open, there are several ways to read the data. Some of the ways will be discussed in future installments of Eddie. This time, we'll use LINE INPUT.

LINE INPUT

As the name suggests, this method of getting data from a file reads one line at a time. A line of text is delimited (divided into chunks) at the "end of line marker." This is signalled in text by a carriage return and line feed combination. It happens when the user who is typing the text into a texteditor presses the ENTER key. LINE INPUT gets the data from one line of a text file into a string variable. LINE INPUT looks like this:

open "myfile.txt" for input as #f
line input #f, varName$
close #f

After this routine, the first line of text in the file is contained in the string variable called varName$.

Here it is in Eddie. The data is placed into the variable LibertyExe$.

'open it for input and read the first line
'into the variable LibertyExe$
open "eddie2.ini" for input as #f
line input #f, LibertyExe$
close #f

A GOSUB routine to get the path to liberty.exe

To keep the code modular (see Lessons with Eddie, we want to keep it in distinct chunks so that it is easy to modify and expand. We're putting the entire routine in one branch label that will be called with GOSUB. The routine does these things:

This can get a little confusing! Here it is in a more complete outline. See if you can match the outline to the code below.



[GetLibertyPath]
    'file to store path info is IniFile$
    'open for append to see if file exists
    'attempting to open a nonexistent file for INPUT causes an error
    open IniFile$ for append as #f
    lenFile = LOF(#f)
    close #f

    'if the length = 0, the file doesn't exist
    if lenFile=0 then
        filedialog "Find Liberty.exe","*liberty*.exe",LibertyExe$
        if LibertyExe$="" then
            'the user cancelled the filedialog
            notice "Liberty.exe not found."
        else
            'the user selected liberty.exe on his computer
            'open the ini file and store the information
            open IniFile$ for output as #f
            print #f, LibertyExe$
            close #f
        end if
    else
        'if the length is greater than 0, the file exists
        'open it for input and read the first line
        'into the variable LibertyExe$
        open IniFile$ for input as #f
        line input #f, LibertyExe$
        close #f
    end if
    RETURN

Nested If/Then

The code above contains a lot of IF, THEN, ELSE, END IF statements. Some of these statements are inside other ones! This is called "nesting." A future installment of the Eddie series will discuss IF, THEN, ELSE, END IF statements.

DEMO

For the Eddie code, go here: Eddie


Home

Tip Corner

API Corner

Youth Corner

SpriteByte: Basics

StyleBits 3

Shaped Window

Recursion

Eddie's Lessons

Beginners Programming

Help

Index